package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.mapper.ActiveDataMapper;
import com.demo.mapper.ActiveMapper;
import com.demo.mapper.LianJiaMapper;
import com.demo.model.ActiveData;
import com.demo.model.ActiveTotalData;
import com.demo.model.LianJia;
import com.demo.service.LianjiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@Service
public class LianjiaServiceImpl implements LianjiaService {
    @Autowired
    private LianJiaMapper lianJiaMapper;
    @Autowired
    private ActiveDataMapper activeDataMapper;
     @Autowired
      private ActiveMapper activeMapper;
    @Override
    public IPage<LianJia> searchByQuYuNew(String quyu, int pageNum, int pageSize) {
        Page<LianJia> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LianJia> wrapper = new QueryWrapper<>();
        wrapper.eq(quyu != null && !quyu.trim().isEmpty(), "qu_yu", quyu);

        // 执行分页查询
        return lianJiaMapper.selectPage(page, wrapper);
    }

    @Override
    public List<ActiveTotalData> searchByJj(String thisEntryDate) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("thisEntryDate", thisEntryDate);
        ActiveTotalData dayCount = activeDataMapper.queryDayCount(map);
        if (dayCount != null){
            dayCount.setId("dayCount");
        }
        ActiveTotalData monCount = activeDataMapper.queryMonCount(map);
        if (monCount != null){
            monCount.setId("monCount");
        }
        ActiveTotalData yearCount = activeDataMapper.queryYearCount(map);
        if (yearCount != null){
            yearCount.setId("yearCount");
        }
        ArrayList<ActiveTotalData> list = new ArrayList<ActiveTotalData>();
        list.add(dayCount);
        list.add(yearCount);
        list.add(monCount);
        return list;
    }

    @Override
    public List<ActiveData> searchByData(String tag, String thisEntryDate) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("thisEntryDate", thisEntryDate);
        //日度分析
        if ("dayCount".equals(tag)) {
            return activeMapper.queryDay(map);
        }
        //月度分析
        else if ("monCount".equals(tag)) {
            List<ActiveData> activeDataList = activeMapper.queryMon(map);
            Iterator<ActiveData> iterator = activeDataList.iterator();
            iterator.next();
            while (iterator.hasNext()) {
                ActiveData next = iterator.next();
                if ("N".equals(next.getIs_monthend())) {
                    iterator.remove();
                }
            }
            return activeDataList;
        }
        //年度分析
        else if ("yearCount".equals(tag)) {
            List<ActiveData> activeDataList = activeMapper.queryYear(map);
            Iterator<ActiveData> iterator = activeDataList.iterator();
            iterator.next();
            while (iterator.hasNext()) {
                ActiveData next = iterator.next();
                if ("N".equals(next.getIs_yearend())) {
                    iterator.remove();
                }
            }
            return activeDataList;
        }
        return null;
    }
}
